home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / editor / EdSpellCheck.js < prev    next >
Encoding:
JavaScript  |  2001-05-10  |  13.0 KB  |  497 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  */
  22.  
  23. var gMisspelledWord;
  24. var spellChecker;
  25. var allowSelectWord = true;
  26. var PreviousReplaceWord = "";
  27. var firstTime = true;
  28.  
  29. // dialog initialization code
  30. function Startup()
  31. {
  32.   if (!InitEditorShell())
  33.     return;
  34.  
  35.   // Get the spellChecker shell
  36.   spellChecker = editorShell.QueryInterface(Components.interfaces.nsIEditorSpellCheck);
  37.   if (!spellChecker) {
  38.     dump("SpellChecker not found!!!\n");
  39.     window.close();
  40.     return;
  41.   }
  42.  
  43.   // Start the spell checker module.
  44.   try {
  45.    spellChecker.InitSpellChecker();
  46.  
  47.    // XXX: We need to read in a pref here so we can set the
  48.    //      default language for the spellchecker!
  49.    // spellChecker.SetCurrentDictionary();
  50.   }
  51.   catch(ex) {
  52.    dump("*** Exception error: InitSpellChecker\n");
  53.     window.close();
  54.     return;
  55.   }
  56.  
  57.   // Create dialog object to store controls for easy access
  58.   dialog = new Object;
  59.   if (!dialog)
  60.   {
  61.     dump("Failed to create dialog object!!!\n");
  62.     Close();
  63.   }
  64.   dialog.MisspelledWordLabel = document.getElementById("MisspelledWordLabel");
  65.   dialog.MisspelledWord      = document.getElementById("MisspelledWord");
  66.   dialog.ReplaceButton       = document.getElementById("Replace");
  67.   dialog.IgnoreButton        = document.getElementById("Ignore");
  68.   dialog.CloseButton         = document.getElementById("Close");
  69.   dialog.ReplaceWordInput    = document.getElementById("ReplaceWordInput");
  70.   dialog.SuggestedList       = document.getElementById("SuggestedList");
  71.   dialog.LanguageMenulist    = document.getElementById("LanguageMenulist");
  72.  
  73.   if (!dialog.MisspelledWord ||
  74.       !dialog.ReplaceWordInput ||
  75.       !dialog.SuggestedList  ||
  76.       !dialog.LanguageMenulist )
  77.   {
  78.     return;
  79.   }
  80.  
  81.   if (dialog.LanguageMenulist)
  82.   {
  83.     // Fill in the language menulist and sync it up
  84.     // with the spellchecker's current language.
  85.  
  86.     var curLang;
  87.  
  88.     try {
  89.       curLang = spellChecker.GetCurrentDictionary();
  90.     } catch(ex) {
  91.       curLang = "";
  92.     }
  93.  
  94.     InitLanguageMenu(curLang);
  95.   }
  96.  
  97.   SetWindowLocation();
  98.  
  99.   // Get the first misspelled word and setup all UI
  100.   NextWord();
  101.  
  102.   // Clear flag that determines message when
  103.   //  no misspelled word is found
  104.   //  (different message when used for the first time)
  105.   firstTime = false;
  106. }
  107.  
  108. function InitLanguageMenu(curLang)
  109. {
  110.   ClearMenulist(dialog.LanguageMenulist);
  111.  
  112.   var o1 = {};
  113.   var o2 = {};
  114.  
  115.   // Get the list of dictionaries from
  116.   // the spellchecker.
  117.  
  118.   try {
  119.     spellChecker.GetDictionaryList(o1, o2);
  120.   } catch(ex) {
  121.     dump("Failed to get DictionaryList!\n");
  122.     return;
  123.   }
  124.  
  125.   var dictList = o1.value;
  126.   var count    = o2.value;
  127.  
  128.   // Load the string bundles that will help us map
  129.   // RFC 1766 strings to UI strings.
  130.  
  131.   var languageBundle;
  132.   var regionBundle;
  133.   var menuStr;
  134.   var menuStr2;
  135.   var isoStrArray;
  136.   var defaultIndex = 0;
  137.  
  138.   // Try to load the language string bundle.
  139.  
  140.   try {
  141.     languageBundle = srGetStrBundle("chrome://global/locale/languageNames.properties");
  142.   } catch(ex) {
  143.     languageBundle = null;
  144.   }
  145.  
  146.   // If we have a language string bundle, try to load the region string bundle.
  147.  
  148.   if (languageBundle)
  149.   {
  150.     try {
  151.       regionBundle = srGetStrBundle("chrome://global/locale/regionNames.properties");
  152.     } catch(ex) {
  153.       regionBundle = null;
  154.     }
  155.   }
  156.  
  157.   for (var i = 0; i < dictList.length; i++)
  158.   {
  159.     try {
  160.       isoStrArray = dictList[i].split("-");
  161.  
  162.       if (languageBundle && isoStrArray[0])
  163.         menuStr = languageBundle.GetStringFromName(isoStrArray[0].toLowerCase());
  164.  
  165.       if (regionBundle && menuStr && isoStrArray.length > 1 && isoStrArray[1])
  166.       {
  167.         menuStr2 = regionBundle.GetStringFromName(isoStrArray[1].toLowerCase());
  168.         if (menuStr2)
  169.           menuStr = menuStr + "/" + menuStr2;
  170.       }
  171.  
  172.       if (!menuStr)
  173.         menuStr = dictList[i];
  174.     } catch (ex) {
  175.       // GetStringFromName throws an exception when
  176.       // a key is not found in the bundle. In that
  177.       // case, just use the original dictList string.
  178.  
  179.       menuStr = dictList[i];
  180.     }
  181.  
  182.     if (curLang && dictList[i] == curLang)
  183.       defaultIndex = i;
  184.  
  185.     AppendLabelAndValueToMenulist(dialog.LanguageMenulist, menuStr, dictList[i]);
  186.   }
  187.  
  188.   // Now make sure the correct item in the menu list is selected.
  189.  
  190.   if (dictList.length > 0)
  191.     dialog.LanguageMenulist.selectedIndex = defaultIndex;
  192. }
  193.  
  194. function DoEnabling()
  195. {
  196.   if (!gMisspelledWord)
  197.   {
  198.     // No more misspelled words
  199.     dialog.MisspelledWord.setAttribute("value",GetString( firstTime ? "NoMisspelledWord" : "CheckSpellingDone"));
  200.  
  201.     dialog.ReplaceButton.removeAttribute("default");
  202.     dialog.IgnoreButton.removeAttribute("default");
  203.  
  204.     dialog.CloseButton.setAttribute("default","true");
  205.     // Shouldn't have to do this if "default" is true?
  206.     dialog.CloseButton.focus();
  207.  
  208.     SetElementEnabledById("MisspelledWordLabel", false);
  209.     SetElementEnabledById("ReplaceWordLabel", false);
  210.     SetElementEnabledById("ReplaceWordInput", false);
  211.     SetElementEnabledById("CheckWord", false);
  212.     SetElementEnabledById("SuggestedListLabel", false);
  213.     SetElementEnabledById("SuggestedList", false);
  214.     SetElementEnabledById("Ignore", false);
  215.     SetElementEnabledById("IgnoreAll", false);
  216.     SetElementEnabledById("Replace", false);
  217.     SetElementEnabledById("ReplaceAll", false);
  218.     SetElementEnabledById("AddToDictionary", false);
  219.   } else {
  220.     SetElementEnabledById("MisspelledWordLabel", true);
  221.     SetElementEnabledById("ReplaceWordLabel", true);
  222.     SetElementEnabledById("ReplaceWordInput", true);
  223.     SetElementEnabledById("CheckWord", true);
  224.     SetElementEnabledById("SuggestedListLabel", true);
  225.     SetElementEnabledById("SuggestedList", true);
  226.     SetElementEnabledById("Ignore", true);
  227.     SetElementEnabledById("IgnoreAll", true);
  228.     SetElementEnabledById("AddToDictionary", true);
  229.  
  230.     dialog.CloseButton.removeAttribute("default");
  231.     SetReplaceEnable();
  232.   }
  233. }
  234.  
  235. function NextWord()
  236. {
  237.   gMisspelledWord = spellChecker.GetNextMisspelledWord();
  238.   SetWidgetsForMisspelledWord();
  239. }
  240.  
  241. function SetWidgetsForMisspelledWord()
  242. {
  243.   dialog.MisspelledWord.setAttribute("value", gMisspelledWord);
  244.  
  245.  
  246.   // Initial replace word is misspelled word
  247.   dialog.ReplaceWordInput.value = gMisspelledWord;
  248.   PreviousReplaceWord = gMisspelledWord;
  249.  
  250.   // This sets dialog.ReplaceWordInput to first suggested word in list
  251.   FillSuggestedList(gMisspelledWord);
  252.  
  253.   DoEnabling();
  254.  
  255.   if (gMisspelledWord)
  256.     SetTextboxFocus(dialog.ReplaceWordInput);
  257. }
  258.  
  259. function CheckWord()
  260. {
  261.   word = dialog.ReplaceWordInput.value;
  262.   if (word) 
  263.   {
  264.     isMisspelled = spellChecker.CheckCurrentWord(word);
  265.     if (isMisspelled)
  266.     {
  267.       FillSuggestedList(word);
  268.       SetReplaceEnable();
  269.     } 
  270.     else 
  271.     {
  272.       ClearTreelist(dialog.SuggestedList);
  273.       var item = AppendStringToTreelistById(dialog.SuggestedList, "CorrectSpelling");
  274.       if (item) item.setAttribute("disabled", "true");
  275.       // Suppress being able to select the message text
  276.       allowSelectWord = false;
  277.     }
  278.   }
  279. }
  280.  
  281. function SelectSuggestedWord()
  282. {
  283.   if (allowSelectWord)
  284.   {
  285.     var index = dialog.SuggestedList.selectedIndex;
  286.     if (index == -1)
  287.     {
  288.       dialog.ReplaceWordInput.value = PreviousReplaceWord;
  289.     }
  290.     else
  291.     {
  292.       var selValue = GetSelectedTreelistValue(dialog.SuggestedList);
  293.       dialog.ReplaceWordInput.value = selValue;
  294.       PreviousReplaceWord = selValue;
  295.     }
  296.     SetReplaceEnable();
  297.   }
  298. }
  299.  
  300. function ChangeReplaceWord()
  301. {
  302.   // Calling this triggers SelectSuggestedWord(),
  303.   //  so temporarily suppress the effect of that
  304.   var saveAllow = allowSelectWord;
  305.   allowSelectWord = false;
  306.  
  307.   // Select matching word in list
  308.   var newIndex = -1;
  309.   var replaceWord = TrimString(dialog.ReplaceWordInput.value);
  310.   if (replaceWord)
  311.   {
  312.     var count = 0;
  313.     var treeChildren = dialog.SuggestedList.firstChild.nextSibling;
  314.     if (treeChildren && treeChildren.childNodes)
  315.       count = treeChildren.childNodes.length;
  316.  
  317.     for (var i = 0; i < count; i++)
  318.     {
  319.       var wordInList = GetTreelistValueAt(dialog.SuggestedList, i);
  320.       if (wordInList == replaceWord)
  321.       {
  322.         newIndex = i;
  323.         break;
  324.       }
  325.     }
  326.   }
  327.   dialog.SuggestedList.selectedIndex = newIndex;
  328.  
  329.   allowSelectWord = saveAllow;
  330.  
  331.   // Remember the new word
  332.   PreviousReplaceWord = dialog.ReplaceWordInput.value;
  333.  
  334.   SetReplaceEnable();
  335. }
  336.  
  337. function Ignore()
  338. {
  339.   NextWord();
  340. }
  341.  
  342. function IgnoreAll()
  343. {
  344.   if (gMisspelledWord) {
  345.     spellChecker.IgnoreWordAllOccurrences(gMisspelledWord);
  346.   }
  347.   NextWord();
  348. }
  349.  
  350. function Replace()
  351. {
  352.   newWord = dialog.ReplaceWordInput.value;
  353.   if (gMisspelledWord && gMisspelledWord != newWord)
  354.   {
  355.     editorShell.BeginBatchChanges();
  356.     isMisspelled = spellChecker.ReplaceWord(gMisspelledWord, newWord, false);
  357.     editorShell.EndBatchChanges();
  358.   }
  359.   NextWord();
  360. }
  361.  
  362. function ReplaceAll()
  363. {
  364.   newWord = dialog.ReplaceWordInput.value;
  365.   if (gMisspelledWord && gMisspelledWord != newWord)
  366.   {
  367.     editorShell.BeginBatchChanges();
  368.     isMisspelled = spellChecker.ReplaceWord(gMisspelledWord, newWord, true);
  369.     editorShell.EndBatchChanges();
  370.   }
  371.   NextWord();
  372. }
  373.  
  374. function AddToDictionary()
  375. {
  376.   if (gMisspelledWord) {
  377.     spellChecker.AddWordToDictionary(gMisspelledWord);
  378.   }
  379.   NextWord();
  380. }
  381.  
  382. function EditDictionary()
  383. {
  384.   window.openDialog("chrome://editor/content/EdDictionary.xul", "_blank", "chrome,close,titlebar,modal", "", gMisspelledWord);
  385. }
  386.  
  387. function SelectLanguage()
  388. {
  389.   var item = dialog.LanguageMenulist.selectedItem;
  390.  
  391.   try {
  392.     spellChecker.SetCurrentDictionary(item.value);
  393.   } catch (ex) {
  394.     dump(ex);
  395.   }
  396. }
  397.  
  398. function Recheck()
  399. {
  400.   //TODO: Should we bother to add a "Recheck" method to interface?
  401.   try {
  402.     var curLang = spellChecker.GetCurrentDictionary();
  403.  
  404.     spellChecker.UninitSpellChecker();
  405.     spellChecker.InitSpellChecker();
  406.     spellChecker.SetCurrentDictionary(curLang);
  407.     gMisspelledWord = spellChecker.GetNextMisspelledWord();
  408.     SetWidgetsForMisspelledWord();
  409.   } catch(ex) {
  410.     dump(ex);
  411.   }
  412. }
  413.  
  414. function FillSuggestedList(misspelledWord)
  415. {
  416.   var list = dialog.SuggestedList;
  417.  
  418.   // Clear the current contents of the list
  419.   allowSelectWord = false;
  420.   ClearTreelist(list);
  421.  
  422.   if (misspelledWord.length > 0)
  423.   {
  424.     // Get suggested words until an empty string is returned
  425.     var count = 0;
  426.     var firstWord = 0;
  427.     do {
  428.       word = spellChecker.GetSuggestedWord();
  429.       if (count==0)
  430.         firstWord = word;
  431.       if (word.length > 0) {
  432.         AppendStringToTreelist(list, word);
  433.         count++;
  434.       }
  435.     } while (word.length > 0);
  436.  
  437.     var len = list.getAttribute("length");
  438.  
  439.     if (count == 0)
  440.     {
  441.       // No suggestions - show a message but don't let user select it
  442.       var item = AppendStringToTreelistById(list, "NoSuggestedWords");
  443.       if (item) item.setAttribute("disabled", "true");
  444.       allowSelectWord = false;
  445.     } else {
  446.       allowSelectWord = true;
  447.       // Initialize with first suggested list by selecting it
  448.       dialog.SuggestedList.selectedIndex = 0;
  449.     }
  450.   } 
  451.   else
  452.   {
  453.     var item = AppendStringToTreelist(list, "");
  454.     if (item)
  455.       item.setAttribute("disabled", "true");
  456.   }
  457. }
  458.  
  459. function SetReplaceEnable()
  460. {
  461.   // Enable "Change..." buttons only if new word is different than misspelled
  462.   var newWord = dialog.ReplaceWordInput.value;
  463.   var enable = newWord.length > 0 && newWord != gMisspelledWord;
  464.   SetElementEnabledById("Replace", enable);
  465.   SetElementEnabledById("ReplaceAll", enable);
  466.   if (enable)
  467.   {
  468.     dialog.ReplaceButton.setAttribute("default","true");
  469.     dialog.IgnoreButton.removeAttribute("default");
  470.   }
  471.   else
  472.   {
  473.     dialog.IgnoreButton.setAttribute("default","true");
  474.     dialog.ReplaceButton.removeAttribute("default");
  475.   }
  476. }
  477.  
  478. function doDefault()
  479. {
  480.   if (dialog.ReplaceButton.getAttribute("default") == "true")
  481.     Replace();
  482.   else if (dialog.IgnoreButton.getAttribute("default") == "true")
  483.     Ignore();
  484.   else if (dialog.CloseButton.getAttribute("default") == "true")
  485.     onClose();
  486. }
  487.  
  488. function onClose()
  489. {
  490.   // Shutdown the spell check and close the dialog
  491.   spellChecker.UninitSpellChecker();
  492.   SaveWindowLocation();
  493.   window.close();
  494.   return true;
  495. }
  496.  
  497.